#include <iostream>
#include <pthread.h>
#include <vector>
#include <string>
#include <cstdlib>
#include <unistd.h>

const int threadnum = 5;

class Task
{
public:
    Task() {}
    void SetData(int x,int y)
    {
        datax = x;
        datay = y;
    }
    int Excute()
    {
        return datax + datay;
    }
    ~Task() {}
private:
    int datax;
    int datay;
};
class ThreadData : public Task
{
public:
    ThreadData(int x,int y,const std::string &threadname)
        :_threadname(threadname)
    {
        _t.SetData(x,y);
    }
    std::string threadname()
    {
        return _threadname;
    }
    int run()
    {
        return _t.Excute();
    }
private:
    std::string _threadname;
    Task _t;
};
class Result
{
public:
    Result() {}
    ~Result() {}
    void SetReslut(int result,const std::string &threadname)
    {
        _result = result;
        _threadname = threadname;
    }
    void Print()
    {
        std::cout << _threadname << " : " << _result << std::endl;
    }
private:
    int _result;
    std::string _threadname;
};
void* handlerTask(void *args)
{
    ThreadData *td = static_cast<ThreadData*>(args);
    std::string name = td->threadname();
    Result *res = new Result();
    int result = td->run();
    res->SetReslut(result,name);
    
    delete td;
    sleep(2);
    return res;
}
int main()
{
    std::vector<pthread_t> threads;
    for(int i = 0;i < threadnum;i++)
    {
        char threadname[64];
        snprintf(threadname,64,"Thread-%d",i + 1);
        ThreadData *td = new ThreadData(10,20,threadname);

        pthread_t tid;
        pthread_create(&tid,nullptr,handlerTask,td);
        threads.push_back(tid);
    }

    std::vector<Result*> result_set;
    void *ret = nullptr;
    for(auto &tid : threads)
    {
        pthread_join(tid,&ret);
        result_set.push_back((Result*)ret);
    }
    for(auto &res : result_set)
    {
        res->Print();
        delete res;
    }

    return 0;
}